package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.advice.RecordLogAnno;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.CountOption;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult findStudent(StudentQueryParam studentQueryParam) {
        Page<Student> page = new Page<>(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> pageResult = studentMapper.findStudent(page, studentQueryParam);
        return new PageResult(pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    @RecordLogAnno
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    @RecordLogAnno
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    @RecordLogAnno
    public void deleteStudent(List<Integer> ids) {
        studentMapper.deleteStudent(ids);
    }

    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id, score);
    }

    @Override
    public CountOption getStudentCountData() {
        List<Map<String, Object>> list = studentMapper.getStudentCountData();
        List<String> jobList = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            jobList.add(map.get("clazzName").toString());
            dataList.add(map.get("nums").toString());
        }
        return new CountOption(jobList, dataList);
    }

    @Override
    public List<Map<String, Object>> countStudentDegree() {
        return studentMapper.countStudentDegree();
    }
}